<?php
/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition End User License Agreement
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magento.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage_Adminhtml
 * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license http://www.magento.com/license/enterprise-edition
 */

/**
 * admin product edit tabs
 *
 * @category   Mage
 * @package    Mage_Adminhtml
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Silk_Qslb_Block_Adminhtml_Catalog_Product_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
    protected $_attributeTabBlock = 'adminhtml/catalog_product_edit_tab_attributes';

    public function __construct()
    {
        parent::__construct();
        $this->setId('product_info_tabs');
        $this->setDestElementId('product_edit_form');
        $this->setTitle(Mage::helper('catalog')->__('Product Information'));
    }

    protected function _prepareLayout()
    {
        $storeId = 0;
        if ($this->getRequest()->getParam('store')) {
            $storeId = Mage::app()->getStore($this->getRequest()->getParam('store'))->getId();
        }

        $session = Mage::getSingleton('admin/session');
        $roleId = $session->getUser()->getRoleId();
        $roles = Mage::helper('zgb')->getUserRoles();
        $skipIds = array(9,10,11,12,17);
        //echo "roleId".$roleId;
        $product = $this->getProduct();

        if (!($setId = $product->getAttributeSetId())) {
            $setId = $this->getRequest()->getParam('set', null);
        }

        if ($setId) {
            $groupCollection = Mage::getResourceModel('eav/entity_attribute_group_collection')
                ->setAttributeSetFilter($setId)
                ->setSortOrder()
                ->load();

            foreach ($groupCollection as $group) {
                if(in_array($group->getId(),$skipIds)) continue;
                if($roleId==Mage::helper('zgb')->getRoleId('warehouse'))
                {
                    if(in_array($group->getId(),array(8,10))) continue;
                }
                $attributes = $product->getAttributes($group->getId(), true);
                // do not add groups without attributes

                foreach ($attributes as $key => $attribute) {
                    if( !$attribute->getIsVisible() ) {
                        unset($attributes[$key]);
                    }

                    // echo get_class($attribute);

                    // if(!$storeId || $storeId==1)
                    // {
                    //     if($attribute->getAttributeCode()=='dealer_id') unset($attributes[$key]);
                    // }

                    // if(in_array($roleId, $roles))
                    // {
                    //    if($attribute->getAttributeCode()=='visibility') unset($attributes[$key]);
                    // }

                    // if(!$product->getId())
                    // {
                    //     if($attribute->getAttributeCode()=='warehouse_id') unset($attributes[$key]);
                    // }

                    //if(!$storeId)
                    //{
                    //    if($attribute->getAttributeCode()=='warehouse_id') unset($attributes[$key]);
                    //    if($attribute->getAttributeCode()=='dealer_id') unset($attributes[$key]);
                    //}

                    //print_r($attribute->getData());
                    //厂商
                    //if($roleId==Mage::helper('zgb')->getRoleId('manufacturer'))
                    //{
                    //    if($attribute->getAttributeCode()=='warehouse_id') unset($attributes[$key]);
                    //    if($attribute->getAttributeCode()=='manufacturer_id') unset($attributes[$key]);
                    //}
                    //经销商
                    //if($roleId==Mage::helper('zgb')->getRoleId('dealer') && $attribute->getAttributeCode()=='dealer_id')
                    //{
                    //    unset($attributes[$key]);
                    //}
                    //
                    //if($roleId!=1 && $attribute->getAttributeCode()=='manufacturer_id')
                    //{
                    //    unset($attributes[$key]);
                    //}
                }


                if (count($attributes)==0) {
                    continue;
                }

                $this->addTab('group_'.$group->getId(), array(
                    'label'     => Mage::helper('catalog')->__($group->getAttributeGroupName()),
                    'content'   => $this->_translateHtml($this->getLayout()->createBlock($this->getAttributeTabBlock(),
                        'adminhtml.catalog.product.edit.tab.attributes')->setGroup($group)
                            ->setGroupAttributes($attributes)
                            ->toHtml()),
                ));
            }

        /*    if($roleId!=Mage::helper('zgb')->getRoleId('manufacturer'))
            {
                if (Mage::helper('core')->isModuleEnabled('Mage_CatalogInventory')) {
                    $this->addTab('inventory', array(
                        'label'     => Mage::helper('catalog')->__('Inventory'),
                        'content'   => $this->_translateHtml($this->getLayout()
                            ->createBlock('adminhtml/catalog_product_edit_tab_inventory')->toHtml()),
                    ));
                }
            }*/

            //经销商和仓库隐藏website和分类设置
            //if($roleId!=Mage::helper('zgb')->getRoleId('warehouse') && $roleId!=Mage::helper('zgb')->getRoleId('dealer'))
            //{
                /**
                 * Don't display website tab for single mode
                 */
           /*     if (!Mage::app()->isSingleStoreMode()) {
                    $this->addTab('websites', array(
                        'label'     => Mage::helper('catalog')->__('Websites'),
                        'content'   => $this->_translateHtml($this->getLayout()
                            ->createBlock('adminhtml/catalog_product_edit_tab_websites')->toHtml()),
                    ));
                }*/

            
              //  $this->addTab('categories', array(
              //      'label'     => Mage::helper('catalog')->__('Categories'),
              //      'url'       => $this->getUrl('*/*/categories', array('_current' => true)),
               //     'class'     => 'ajax',
              //  ));

            //}
            if(false)
            {
                $this->addTab('related', array(
                    'label'     => Mage::helper('catalog')->__('Related Products'),
                    'url'       => $this->getUrl('*/*/related', array('_current' => true)),
                    'class'     => 'ajax',
                ));

                $this->addTab('upsell', array(
                    'label'     => Mage::helper('catalog')->__('Up-sells'),
                    'url'       => $this->getUrl('*/*/upsell', array('_current' => true)),
                    'class'     => 'ajax',
                ));

                $this->addTab('crosssell', array(
                    'label'     => Mage::helper('catalog')->__('Cross-sells'),
                    'url'       => $this->getUrl('*/*/crosssell', array('_current' => true)),
                    'class'     => 'ajax',
                ));
            }
            
            

            $alertPriceAllow = Mage::getStoreConfig('catalog/productalert/allow_price');
            $alertStockAllow = Mage::getStoreConfig('catalog/productalert/allow_stock');

            if (($alertPriceAllow || $alertStockAllow) && !$product->isGrouped()) {
                $this->addTab('productalert', array(
                    'label'     => Mage::helper('catalog')->__('Product Alerts'),
                    'content'   => $this->_translateHtml($this->getLayout()
                        ->createBlock('adminhtml/catalog_product_edit_tab_alerts', 'admin.alerts.products')->toHtml())
                ));
            }

            if(false):
            if( $this->getRequest()->getParam('id', false) ) {
                if (Mage::helper('catalog')->isModuleEnabled('Mage_Review')) {
                    if (Mage::getSingleton('admin/session')->isAllowed('admin/catalog/reviews_ratings')){
                        $this->addTab('reviews', array(
                            'label' => Mage::helper('catalog')->__('Product Reviews'),
                            'url'   => $this->getUrl('*/*/reviews', array('_current' => true)),
                            'class' => 'ajax',
                        ));
                    }
                }
                if (Mage::helper('catalog')->isModuleEnabled('Mage_Tag')) {
                    if (Mage::getSingleton('admin/session')->isAllowed('admin/catalog/tag')){
                        $this->addTab('tags', array(
                         'label'     => Mage::helper('catalog')->__('Product Tags'),
                         'url'   => $this->getUrl('*/*/tagGrid', array('_current' => true)),
                         'class' => 'ajax',
                        ));

                        $this->addTab('customers_tags', array(
                            'label'     => Mage::helper('catalog')->__('Customers Tagged Product'),
                            'url'   => $this->getUrl('*/*/tagCustomerGrid', array('_current' => true)),
                            'class' => 'ajax',
                        ));
                    }
                }

            }
            endif;
            if($roleId!=Mage::helper('zgb')->getRoleId('warehouse')):
            /**
             * Do not change this tab id
             * @see Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs_Configurable
             * @see Mage_Bundle_Block_Adminhtml_Catalog_Product_Edit_Tabs
             */
          //  if (!$product->isGrouped()) {
          //      $this->addTab('customer_options', array(
          //          'label' => Mage::helper('catalog')->__('Custom Options'),
          //          'url'   => $this->getUrl('*/*/options', array('_current' => true)),
          //          'class' => 'ajax',
          //    ));
           // }
            endif;

        }
        else {
            $this->addTab('set', array(
                'label'     => Mage::helper('catalog')->__('Settings'),
                'content'   => $this->_translateHtml($this->getLayout()
                    ->createBlock('adminhtml/catalog_product_edit_tab_settings')->toHtml()),
                'active'    => true
            ));
        }
        if (Mage::helper('core')->isModuleEnabled('Mage_CatalogInventory')) {
            $this->addTab('inventory', array(
                'label'     => Mage::helper('catalog')->__('Inventory'),
                'content'   => $this->_translateHtml($this->getLayout()
                    ->createBlock('adminhtml/catalog_product_edit_tab_inventory')->toHtml()),
            ));
        }
        if(Mage::getSingleton('admin/session')->isAllowed('catalog/inventoryUpdate'))
            {
            $this->addTab('warehouse_inventory', array(
                'label'=> "出入库",
                'content'   => $this->_translateHtml($this->getLayout()
                                ->createBlock('zgb/adminhtml_product_tab_warehouse')->toHtml())
            ));
        }
        return parent::_prepareLayout();
    }

    /**
     * Retrive product object from object if not from registry
     *
     * @return Mage_Catalog_Model_Product
     */
    public function getProduct()
    {
        if (!($this->getData('product') instanceof Mage_Catalog_Model_Product)) {
            $this->setData('product', Mage::registry('product'));
        }
        return $this->getData('product');
    }

    /**
     * Getting attribute block name for tabs
     *
     * @return string
     */
    public function getAttributeTabBlock()
    {
        if (is_null(Mage::helper('adminhtml/catalog')->getAttributeTabBlock())) {
            return $this->_attributeTabBlock;
        }
        return Mage::helper('adminhtml/catalog')->getAttributeTabBlock();
    }

    public function setAttributeTabBlock($attributeTabBlock)
    {
        $this->_attributeTabBlock = $attributeTabBlock;
        return $this;
    }

    /**
     * Translate html content
     *
     * @param string $html
     * @return string
     */
    protected function _translateHtml($html)
    {
        Mage::getSingleton('core/translate_inline')->processResponseBody($html);
        return $html;
    }
}
